<?php


namespace app\index\controller;


use app\BaseController;
use app\CommonController;
use app\index\model\GoodsProp;
use app\index\model\GoodsPropValue;
use think\Collection;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\Paginator;

class Brand extends BaseController
{
    public function tryone()
    {
        //接参
        $param  = Request::get(['pid','pname','pvalues','status','sort_order'],'','trim,htmlspecialchars,strip_tags');
        unset($param['id']);
        //调用模型类方法
        $propValus = \app\index\model\GoodsPropValue::getGoodsPropValues();
        $list = \app\index\model\GoodsProp::getGoodsProp($propValus,$param);

        //模板渲染
        $empty = "<tr><td colspan='8' align='center'>暂时没有数据</td></tr>";
        View::assign('empty',$empty);
//        View::assign('data',$data);

        //模板赋值
        return View::fetch('',['propValus'=>$propValus,'param'=>$param,'list'=>$list,'empty'=>$empty]);
    }

    public function tryone_add()
    {
        if(Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');
            $post['pvalues'] = implode(',',$post['prop_value']);
            $brandObj = new \app\index\model\GoodsProp();
            $result = $brandObj->propAdd($post);
            if($result and $post['button']=="提交并继续"){
                $this->success("成功！ID为:".$brandObj->getInsertId());
            }else if($result and $post['button']=="提交并返回"){
                $this->success("成功！ID为:".$brandObj->getInsertId(),url('tryone'));
            }else{
                $this->error("失败！原因:".$brandObj->getError());
            }
        }else{
            //  调用模型类获取数据
            $props = Db::name('goods_prop_value')->select();
            // 渲染，传参
            return View::fetch('',['props'=>$props]);
        }
    }

    public function changetryone()
    {
        if(Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');
            $post['pvalues'] = $post['prop_value'];
            $tryoneObj = new \app\index\model\GoodsProp();
            $result = $tryoneObj->changetryone($post);
            if($result){
                $this->success("成功！",url('tryone',Request::get()));
            }else{
                $this->error("失败！");
            }
        }else{
            //  显示修改的页面
            $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
            $tryoneObj = \app\index\model\GoodsProp::find($id);
            if (empty($tryoneObj)){
                $this->error('参数错误!',url('tryone'));
            }
            //  调用模型类获取数据
            $props = Db::name('GoodsPropValue')->select();
            View::assign($tryoneObj->toArray());
            // 渲染，传参
            return View::fetch('',['props'=>$props]);
        }
    }

    public function trytwo()
    {
        // 1 接参，过滤
        $brand_name=Request::get('brand_name','','trim,htmlspecialchars,strip_tags');
        $brand_logo=Request::get('brand_logo/a',[],'trim,htmlspecialchars,strip_tags');
        $recommended=Request::get('recommended','2','trim,htmlspecialchars,strip_tags');
        $data = [
            'brand_name' => $brand_name,
            'brand_logo' => $brand_logo,
            'recommended' => $recommended,
        ];
        // 2 调用模型类获取数据
        $list = \app\index\model\Brand::getBrand($data,$data);

        // 3 模版赋值
        $empty = "<tr><td colspan='8' align='center'>暂时没有数据</td></tr>";
        View::assign('empty',$empty);
        View::assign('data',$data);

        // 4 模版渲染
        return View::fetch('',['list'=>$list]);
    }


    public function trytwo_add()
    {

        if(Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');
            // 文件上传
            try{
                // 上传验证
                $image = Request::file('brand_logo','');
                $rule = ['brand_logo'=>'filesize:40960000|fileExt:jpg,png|fileMime:image/jpeg,image/png'];
                $data = ['brand_logo'=>$image];
                // 验证器的方法不返回参数，如果错误会抛出异常
                validate($rule)->check($data);
                // 验证通过 执行上传
                $savename = \think\facade\Filesystem::disk('galanzi')->putFile('',$image,'get_uploadfile_name');
                $post['brand_logo'] = str_replace('\\','/',$savename);
            }catch(\think\exception\ValidateException $e){
                $this->error($e->getMessage());
            }catch(\think\Exception $e){}
            $brandObj = new \app\index\model\Brand();
            $result = $brandObj->brandAdd($post);
            if($result and $post['button']=="提交并继续"){
                $this->success("成功！ID为:".$brandObj->getInsertId());
            }else if($result and $post['button']=="提交并返回"){
                $this->success("成功！ID为:".$brandObj->getInsertId(),url('trytwo'));
            }else{
                $this->error("失败！原因:".$brandObj->getError());
            }
        }else{
            //  调用模型类获取数据
            $props = Db::name('brand')->select();
            // 渲染，传参
            return View::fetch('',['props'=>$props]);
        }

    }
    public function changetrytwo()
    {
        if(Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');
            // 文件上传
            try{
                // 上传验证
                $image = Request::file('brand_logo','');
                $rule = ['brand_logo'=>'filesize:40960000|fileExt:jpg,png|fileMime:image/jpeg,image/png'];
                $data = ['brand_logo'=>$image];
                // 验证器的方法不返回参数，如果错误会抛出异常
                validate($rule)->check($data);
                // 验证通过 执行上传
                $savename = \think\facade\Filesystem::disk('galanzi')->putFile('',$image,'get_uploadfile_name');
                $post['brand_logo'] = str_replace('\\','/',$savename);
            }catch(\think\exception\ValidateException $e){
                $this->error($e->getMessage());
            }catch(\think\Exception $e){}

            $trytwoObj = new \app\index\model\Brand();
            $result = $trytwoObj->changetrytwo($post);

            if($result){
                $this->success("成功！",url('trytwo',Request::get()));
            }else{
                $this->error("失败！");
            }
        }else{
            //  显示修改的页面
            $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
            $trytwoObj = \app\index\model\Brand::find($id);
            if (empty($trytwoObj)){
                $this->error('参数错误!',url('trytwo'));
            }
            //  调用模型类获取数据
            $props = Db::name('Brand')->select();
            View::assign($trytwoObj->toArray());
            // 渲染，传参
            return View::fetch('',['props'=>$props]);
        }
    }


}